package com.xhg.mqtt.listener;

import com.xhg.mqtt.constant.MsgConstants;
import com.xhg.mqtt.core.MqttSimpleClient;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 连接成功监听
 * @author E.T
 * @version 1.0
 * @Description
 * @date 2018-08-20 14:26
 * @updateby
 * @updatedate
 * @since 1.0
 */
public class ConnectionListener {

    private volatile boolean flag = true;

    public void startListen() {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.execute(() -> {
            Thread.currentThread().setName("连接监听线程");
            for (; ; ) {
                //mqtt连接成功监听
                if (flag) {
                    if (MqttSimpleClient.getInstance() != null) {
                        try {
                            MessageManager.connectComplete(MqttSimpleClient.getInstance().isConnect());
                        } catch (Exception e) {
                            MqttLoggerFactory.error(MsgConstants.NO_INSTACE_ERROR, e);
                        }
                        flag = false;
                        exec.shutdown();
                        break;
                    }
                }
            }
        });
    }
}
